/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.app;

import com.railway.utils.RestServer;
import com.railway.utils.Toast;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.joda.time.DateTime;
import javax.swing.JFrame;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import org.jfree.util.Log;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Seconds;
/**
 *
 * @author Kenny
 */
public class Test extends JFrame{

    public static String generateRandomPassword() {
        SecureRandom random = new SecureRandom();
        return new BigInteger(35, random).toString(10);
    }
public static void copyFiles22(File sourceLocation , File targetLocation)throws IOException {
        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }
            File[] files = sourceLocation.listFiles();
            for(File file:files){
                InputStream in = new FileInputStream(file);
                OutputStream out = new FileOutputStream(targetLocation+"/"+file.getName());

                // Copy the bits from input stream to output stream
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            }            
        }
    }

public static void copyFile(File oldLocation, File newLocation) throws IOException {
        if ( oldLocation.exists( )) {
            BufferedInputStream  reader = new BufferedInputStream( new FileInputStream(oldLocation) );
            BufferedOutputStream  writer = new BufferedOutputStream( new FileOutputStream(newLocation, false));
            try {
                byte[]  buff = new byte[8192];
                int numChars;
                while ( (numChars = reader.read(  buff, 0, buff.length ) ) != -1) {
                    writer.write( buff, 0, numChars );
                }
            } catch( IOException ex ) {
                throw new IOException("IOException when transferring " + oldLocation.getPath() + " to " + newLocation.getPath());
            } finally {
                try {
                    if ( reader != null ){                      
                        writer.close();
                        reader.close();
                    }
                } catch( IOException ex ){
                    System.out.println( "Error closing files when transferring " + oldLocation.getPath() + " to " + newLocation.getPath() ); 
                }
            }
        } else {
            throw new IOException("Old location does not exist when transferring " + oldLocation.getPath() + " to " + newLocation.getPath() );
        }
    }  
    
    public static void main(String[] args) { 

//        try{
//            File src = new File("C:\\Users\\jason1\\Desktop\\privatemsg.txt");
//        File des = new File("C:\\Users\\jason1\\Desktop\\a\\aa.txt");
//        copyFile(src,des);
//        }catch(IOException e){
//            System.out.println(e);
//        }
//        Map<String, Object> data = new HashMap<>();
//        data.put("shid","6");
//        data.put("status", "AMD");
//        data.put("departureDate", "2013-11-26");
//        data.put("class", "EG06");
//        data.put("departure","5:09:00");
//        data.put("referenceNo","78");
//        
//        String a = RestServer.httpGet("GetTicketCodeByRef", "70");
//        System.out.println(a);
//        try {
//            JSONObject json = new JSONObject(a);
//            //JSONObject content = json.getJSONObject("content");
//            System.out.println(json.get("status"));
//        } catch (JSONException ex) {
//            System.out.println(ex);
//        }
//        
//        
//
//        String test = "ipoh-kl";
//        System.out.println(test);
//        System.out.println("last index of - is " + test.lastIndexOf("-"));
//        String reverse = test.substring(test.lastIndexOf("-") + 1) + "-" + test.substring(0, test.lastIndexOf("-"));
//        System.out.println(reverse);
//        Calendar c = Calendar.getInstance();
//        c.set(1970, 0, 1, 0, 0, 0);
//        Date date1 = c.getTime();
//        Date date2 = c.getTime();
//        System.out.println(date1 + "\n" + date2 + "\n" + " 1 to 2 " + date1.compareTo(date2));
//        System.out.println(date1 + "\n" + date2 + "\n" + "2 to 1 " + date2.compareTo(date1));
//        System.out.println(date1 + "\n" + date2 + "\n" + "1 to 1 " + date1.compareTo(date1));
//        System.out.println(date1 + "\n" + date2 + "\n" + "2 to 2 " + date2.compareTo(date2));
//        Map<String, Object> data = new HashMap<>();
//        data.put("uid","8");
//        data.put("question", "What was your childhood nickname?");
//        data.put("answer", "Nosaj");
//        data.put("password",""+generateRandomPassword());
//        
//        String a = RestServer.httpPost("forgetPassword", data);
//        System.out.println(a);
//        try {
//            JSONObject json = new JSONObject(a);
//            JSONObject content = json.getJSONObject("content");
//            
//        } catch (JSONException ex) {
//        }
//        
        Map<String, Object> dataLog = new HashMap<>();
        dataLog.put("amount", "100");
        dataLog.put("madeBy","2");
        dataLog.put("type","walk-in");
        
        
        
        String b = RestServer.httpPost("insertSalesLog",dataLog);
        try{
            System.out.println(b);
            JSONObject json = new JSONObject(b);
            
        }catch (JSONException ex) {
            JOptionPane.showMessageDialog(null,"error");
        }
//        
//        String a = RestServer.httpGet("discount", "");
//        System.out.println(a);
//        try{
//            JSONObject json = new JSONObject(a);
//            JSONArray ja = json.getJSONArray("content");
//            System.out.println(ja.getJSONObject(0).get("rate"));
//        }catch(JSONException ex){
//        }
    }

}
